package com.faner.spider

import com.fun.frame.httpclient.FanLibrary
import com.fun.utils.Regex
import net.sf.json.JSONObject

class News extends FanLibrary {
    static JSONObject getItNews() {
        def response = getHttpResponse(getHttpGet("https://www.ithome.com/list/")).getString("content")
        def all = Regex.regexAll(response, "<li>\\s+<str.*\\s+.*\\s+.*</li>")
        def object = new JSONObject()
        int i = 0
        all.each { news ->
            news = news.replaceAll("\\s", EMPTY)
            if (!news.contains("广告") && i < 30) {
                def url = Regex.regexAll(news, "https:.*htm")[0]
                def title = Regex.regexAll(news, "blank\".*>*.*>.*</a>")[0]
                title = title.substring(7).replaceAll("<.+?>", EMPTY)
                i++
                object.put(title, url)
            }
        }
        return object;
    }

    static JSONObject getSina() {
        def object = new JSONObject()
        def response = getHttpResponse(getHttpGet("https://news.sina.com.cn/")).getString("content")
        def all = Regex.regexAll(response, "<a target=\"_blank\" href=\"https://.*?</a>")
        int i = 0
        all.each { line ->
            def url = Regex.getRegex(line, "<a target=\"_blank\" href=\".*?\"")
            def title = Regex.getRegex(line, ">.*?<")
            if (!url.contains("weibo") && i < 30) {
                object.put(title, url)
                i++
            }
        }
        return object;
    }

    static wy() {
        JSONObject result = new JSONObject()
        def s = getHttpResponse(getHttpGet("https://news.163.com/")).getString("content")
        def all = Regex.regexAll(s, "<a href=\"http.*?\\/.*?/.*?\">.{7,20}?</a>")
        int i = 0
        all.each { line ->
            if (i < 30 && !line.contains("span") && !line.contains("新闻") && !line.contains("网易") && !line.contains("|") && !line.contains("class") && !line.contains("About") && !line.contains("photoview")) {
                def url = Regex.getRegex(line, "=\".*?\"")
                def title = Regex.getRegex(line, ">.*<")
                result.put(title, url)
                i++
            }
        }
        return result
    }
}